public without sharing class EmailHelper {
	 
		public enum convTo
		 {
			B ,
			KB ,
			MB ,
			GB ,
			//Enumerations for file size conversions
			TB ,
			PB ,
			EB ,
			ZI ,
			YI 
		}
		  
        public enum BodyTextType
        {
        	PlainText,
        	Html
        }
        public static void SendMailfromTemplate(Lead[]  emailLeads, string templateName )
        {
            List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
            for(Lead lead : emailLeads){                       
                    //send email 
                    Messaging.SingleEmailMessage nEmail;
                    nEmail = new Messaging.SingleEmailMessage();
                    if(lead.Id != null){nEmail.setWhatId(lead.Id);}
                    nEmail.setTargetObjectId(lead.Id);
                    messages.add(nEmail);
               }                          
      
        if(!messages.isEmpty()){
            EmailTemplate[] emailTemplates = [select Id from EmailTemplate where Name = :templateName and isActive = true];
            for(Messaging.SingleEmailMessage e : messages){
                  e.setTemplateId(emailTemplates[0].Id);          
            }
            
            Integer size = messages.size();
            Messaging.SingleEmailMessage[] emails = new Messaging.SingleEmailMessage[size];
            emails.addAll(messages);
            try{
                  Messaging.sendEmail(emails);
            }catch(Exception ex){
                  System.debug('Send Email failed');
            }
      }
        
     }
		public static void SendMessage(string subject, string[] toAddresses, string[] ccAddresses, string bodyText, integer  bodyTextType)
			{
				// that will send out a single email to the addresses in the To, CC & BCC list.  				    
				Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
				
				// Assign the addresses for the To and CC lists to the mail object.
				if(toAddresses.size() > 0)  				    
				    mail.setToAddresses(toAddresses);
				
				if(ccAddresses!=null && ccAddresses.size() > 0)
				   mail.setCcAddresses(ccAddresses);
				
				// Specify the address used when the recipients reply to the email.   				    
				mail.setReplyTo('noreply@attinteractive.com');
				
				// Specify the name used as the display name.  				    
				mail.setSenderDisplayName('Salesforce Support');
				
				// Specify the subject line for your email address.  				    
				mail.setSubject(subject);
				
				// Set to True if you want to BCC yourself on the email.  				    
				mail.setBccSender(false);
				
				// Optionally append the salesforce.com email signature to the email.  				    
				// The email address of the user executing the Apex Code will be used.  				    
				mail.setUseSignature(false);
				
				// Specify the text content of the email.
				//if( bodyTextType == BodyTextType.PlainText)  				    
				if( bodyTextType == 1)
					mail.setPlainTextBody(bodyText);
				else if( bodyTextType == 2)					
				    mail.setHtmlBody(bodyText);
	     				
				// Send the email you have created.  				    
				Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
			}
			

private static decimal ConvertBytes(double bytes, convTo convertTo)
{
	    integer val = (integer)convertTo.Ordinal();
		return bytes / (Math.Pow(1024,  val));	
}

//Convert attachment size to display in bytes,KB, Megabytes etc for related lists  
public static string ConvertSize(Integer fileSize)
{
	double sizeOfKB = 1024;
	// Actual size in bytes of 1KB
	double sizeOfMB = 1048576;
	// 1MB
	double sizeOfGB = 1073741824;
	// 1GB
	double sizeOfTB = 1099511627776L;
	// 1TB
	double sizeofPB = 1125899906842624L;
	// 1PB
	
	decimal tempFileSize = 0;
	string tempFileSizeString = null;

	//Filesize is in Bytes
	if (fileSize < sizeOfKB) {
		tempFileSize = ConvertBytes(fileSize, convTo.B);
		if (tempFileSize == -1)
			return null;
		//Return our converted value
		return Math.Round(tempFileSize) + ' bytes ' ;

	//Filesize is in Kilobytes
	} else if (fileSize >= sizeOfKB & fileSize < sizeOfMB) {
		tempFileSize = ConvertBytes(fileSize, convTo.KB);
		if (tempFileSize == -1)
			return null;
		return Math.Round(tempFileSize) + 'KB ';

	// Filesize is in Megabytes
	} else if (fileSize >= sizeOfMB & fileSize < sizeOfGB) {
		tempFileSize = ConvertBytes(fileSize, convTo.MB);
		if (tempFileSize == -1)
			return null;
		return tempFileSize.setScale(2) + 'MB ';

	//Filesize is in Gigabytes
	} else if (fileSize >= sizeOfGB & fileSize < sizeOfTB) {
		tempFileSize = ConvertBytes(fileSize, convTo.GB);
		if (tempFileSize == -1)
			return null;
		return tempFileSize.setScale(2) + 'GB ' ;

	//Filesize is in Terabytes
	} else if (fileSize >= sizeOfTB & fileSize < sizeofPB) {
		tempFileSize = ConvertBytes(fileSize, convTo.TB);
		if (tempFileSize == -1)
			return null;
		return tempFileSize.setScale(2) + 'TB ' ;

	//Anything bigger than that is silly ;) - not handled in this code !

	} else {
		return null;
		//Invalid filesize so return Nothing

	}

}
		
			
}